package pku.ss.news.dataAccess;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import java.util.List;



public class SubscriptionDAO {
	
	public List<String> getSubscription(String userId){
		Connection con=DBConnector.getConnection(); 
		String sql="select keyword from subscription where userId=?";
		try {
			PreparedStatement st= con.prepareStatement(sql);
			st.setString(1, userId);
			ResultSet rs=st.executeQuery();
			ArrayList<String> list=new ArrayList<String>();
			while(rs.next()){
				list.add(rs.getString("keyword"));
			}
			
			st.close();
			con.close();
			return list;
		} catch (SQLException e) {
			return null;
		}
	
	}
	public void insertSubscription(String userId, String keyword){
		Connection con=DBConnector.getConnection(); 
		String sql="insert into subscription(userId, keyword) values(?,?)";
		try {
			PreparedStatement st= con.prepareStatement(sql);
			st.setString(1, userId);
			st.setString(2, keyword);
			st.execute();
			
			st.close();
			con.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void deleteSubscription(String userId, String keyword){
		Connection con=DBConnector.getConnection(); 
		String sql="delete subscription where userId=? and keyword=?";
		try {
			PreparedStatement st= con.prepareStatement(sql);
			st.setString(1, userId);
			st.setString(2, keyword);
			st.execute();
			
			st.close();
			con.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
}
